package com.thunbu.thunbu_meeting_system.activity

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.cathu.lib_commonkt.base.BaseMVVMActivity
import com.google.android.material.tabs.TabLayout
import com.google.gson.Gson
import com.thunbu.lib_common.widget.CommonToolBar
import com.thunbu.thunbu_meeting_system.*
import com.thunbu.thunbu_meeting_system.bean.StockModel
import com.thunbu.thunbu_meeting_system.databinding.ActivityInventoryTaskListBinding
import com.thunbu.thunbu_meeting_system.databinding.InventoryTaskListItemBinding
import com.thunbu.thunbu_meeting_system.viewMdel.InventoryTaskListViewModel
import java.util.*

/**
 * 盘点任务
 */
class InventoryTaskListActivity : BaseMVVMActivity<ActivityInventoryTaskListBinding,
        InventoryTaskListViewModel>(){

    lateinit var toolBar: CommonToolBar

    private lateinit var mAdapter: BaseVBQuickAdapter<StockModel, InventoryTaskListItemBinding>
    val ORIGINAL_PAGE = 1
    var mPage = ORIGINAL_PAGE
    var currentIndex = 0

    override fun onMVVMCreated() {
        initToolBar()
        initView()
        initRv()
        initVM()
    }

    override fun onResume() {
        super.onResume()
        reset()
        loadData()
    }

    private fun initVM() {
        vm.stockPage.observe(this){
            bindViews.refereshLayout.finishLoadMore()
            if (mPage == ORIGINAL_PAGE) {
                mAdapter.setNewData(it?.list)
            } else {
                mAdapter.addData(it?.list?: mutableListOf())
            }
            if(it?.total?:0 > mAdapter.itemCount+1){
                mPage++
            }else{
                bindViews.refereshLayout.setEnableLoadMore(false)
            }
        }
    }

    private fun loadData(){
        vm.requestStockList(currentIndex,mPage,20)
    }

    private fun initRv() {
        bindViews.rlList.layoutManager = LinearLayoutManager(this)
        mAdapter = bindViews.rlList.bindAdapter { item, position ->
            tvTitle.text=item.stockTaskName
            tvTitleCount.text =item.count?.toString()
            when(item.stockStatus){
                0 ->  this.tvTitleStatus.text = "未盘点"
                1 ->  this.tvTitleStatus.text = "盘点中"
                2 ->  this.tvTitleStatus.text = "已完成盘点"
                3 ->  this.tvTitleStatus.text = "已终止盘点"
            }
            llStartPd.visibility = (item.stockStatus == 2 || item.stockStatus == 3).ternary(View.GONE,View.VISIBLE)
            this.tvTitleDate.text=item.createTime
            tvLayout1Dpd.text=item.toBeCounted?.toString()
            this.tvLayout2Pcs.text=item.inventoryCount?.toString()
            this.tvLayout3Pys.text=item.diskSurplusCount?.toString()
            this.tvLayout4Pks.text=item.orderLossCount?.toString()

//            ivIcon.setImageResource(item.icon)
//            tvName.text = item.text
//            tvCount.text = item.value.toString()
            llStartPd.setOnClickListener {
                gotoActivity<InventoryActivity>{
                    putExtra(InventoryActivity.STOCKTASK_CODE, Gson().toJson(item))
                }
            }
            root.setOnClickListener {
                gotoActivity<InventoryActivity>{
                    putExtra(InventoryActivity.STOCKTASK_CODE, Gson().toJson(item))
                    putExtra(InventoryActivity.IS_EDIT, !(item.stockStatus == 2 || item.stockStatus == 3))
                }
            }
        }
    }

    private fun initView(){
        bindViews.refereshLayout.setEnableRefresh(false)
        bindViews.refereshLayout.setEnableLoadMoreWhenContentNotFull(false)
        bindViews.refereshLayout.setEnableLoadMore(true)
        bindViews.refereshLayout.setOnLoadMoreListener {
            loadData()
        }
        bindViews.tlChange.addOnTabSelectedListener(object :TabLayout.OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab) {
                checkTab(tab.position)
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {

            }

            override fun onTabReselected(tab: TabLayout.Tab) {

            }

        })
    }

    private fun checkTab(clickIndex:Int){
        if(currentIndex != clickIndex){
            currentIndex = clickIndex
            reset()
            loadData()
        }
    }

    private fun reset(){
        mPage = ORIGINAL_PAGE
        bindViews.refereshLayout.setEnableLoadMore(true)
    }



    private fun initToolBar() {
        toolBar = setToolBar(CommonToolBar::class.java)
        toolBar.setBackAction { finish() }
        toolBar.setTitle(getString(R.string.title1))
    }

}